﻿$(function () {
    //$('#admin').modal("show");
    var servers;

    setTimeout(function () {
        $("#pwd").focus();
    }, 2000);

    $(document).on('keydown', function (e) {
        if (e.which == 13) { // 检查按下的是否是回车键（Enter）
            $('#ok').click(); // 调用按钮的点击事件
        }
    });

    //$("#admin").on("hidden.bs.modal", function () {
    //    if ($("#pwd").val() != "liuc") {
    //        //alert("密码不对");
    //        location.href = "index.html";
    //    } else {
    //        $('#admin').modal("hide");
    //    }
    //})
    //加载网页后获取服务器列表
    $.ajax({
        url: "/ashx/adHandler.ashx",
        type: "POST",
        dataType: "json",
        data: { action: "GetList" },
        success: function (data) {
            if (data.Success) {
                servers = $.parseJSON(data.Data);
                $.each(servers, function (idx, v) {
                    $("#selserver").append("<option value='" + idx + "'>" + v.domain + "</option>");
                });
                ServerSelected(servers, 0);
            } else {
                alert(data.Message);
            }
        }
    });
    //select事件
    $("#selserver").change(function () {
        ServerSelected(servers, $(this).val());
    });

    //重置按钮点击
    $("#btnreset").click(function () {
        var username = $("#username").val().trim();
        if (username.length<1) {
            alert("账号不能为空");
            $("#username").focus();
            return;
        }

        var $this = $(this);
        $this.prop('disabled', true);

        var oupath = CheckandJoin();
        if (oupath.length < 1) {
            alert("未知错误，请联系管理员");
            return;
        }
        $.ajax({
            url: "/ashx/adHandler.ashx",
            type: "POST",
            data: { action: "Reset", username: username, oupath: oupath, op: $("#op").val().trim(), oppwd: $("#oppwd").val().trim() },
            success: function (data) {
                alert(data);
                $this.prop('disabled', false);
            }
        });
    });
    //创建按钮点击
    $("#btncreate").click(function () {
        var username = $("#username").val().trim();
        if (username.length < 1) {
            alert("账号不能为空");
            $("#username").focus();
            return;
        }

        var $this = $(this);
        $this.prop('disabled', true);

        var oupath = CheckandJoin();
        if (oupath.length<1) {
            alert("未知错误，请联系管理员");
            return;
        }
        $.ajax({
            url: "/ashx/adHandler.ashx",
            type: "POST",
            data: { action: "Create", username: username, oupath: oupath, op: $("#op").val().trim(), oppwd: $("#oppwd").val().trim() },
            success: function (data) {
                alert(data);
                $this.prop('disabled', false);
            }
        });
    });
    //删除按钮点击
    $("#btndelete").click(function() {
        var username = $("#username").val().trim();
        if (username.length < 1) {
            alert("账号不能为空");
            $("#username").focus();
            return;
        }

        if (confirm("确定要删除"+username+"吗？")) {
            var $this = $(this);
            $this.prop('disabled', true);

            var oupath = CheckandJoin();
            if (oupath.length < 1) {
                alert("未知错误，请联系管理员");
                return;
            }
            $.ajax({
                url: "/ashx/adHandler.ashx",
                type: "POST",
                data: { action: "Delete", username: username, oupath: oupath, op: $("#op").val().trim(), oppwd: $("#oppwd").val().trim() },
                success: function (data) {
                    alert(data);
                    $this.prop('disabled', false);
                }
            });
        }
    });
    //显示按钮点击
    $("#btngetusers").click(function () {
        $("#tb_result").html("");
        var $this = $(this);
        $this.prop('disabled', true);

        var oupath = CheckandJoin();
        if (oupath.length < 1) {
            alert("未知错误，请联系管理员");
            return;
        }
        $.ajax({
            url: "/ashx/adHandler.ashx",
            type: "POST",
            dataType: "json",
            data: { action: "GetUsers", oupath: oupath, op: $("#op").val().trim(), oppwd: $("#oppwd").val().trim() },
            success: function (data) {
                if (data.Success) {
                    iniTable($.parseJSON(data.Data));
                    $this.prop('disabled', false);
                } else {
                    alert(data.Message);
                }
            }
        });
    });

});

//Select变，各文本框跟着变
function ServerSelected(serverlist, index) {
    $("#controller").val(serverlist[index].controller);
    $("#op").val(serverlist[index].op);
    $("#oppwd").val(serverlist[index].oppwd);
    $("#ou1").val(serverlist[index].OU1);
    $("#ou2").val(serverlist[index].OU2);
    $("#ou3").val(serverlist[index].OU3);
}

//字符串拼接
function CheckandJoin() {
    var domain = $("#selserver option:selected").text().trim()
    var controller = $("#controller").val().trim();
    var op = $("#op").val().trim();
    var oppwd = $("#oppwd").val().trim();
    var ou1 = $("#ou1").val().trim();
    var ou2 = $("#ou2").val().trim();
    var ou3 = $("#ou3").val().trim();
    //有效性检查
    if (controller.length < 1) {
        alert("控制器必须填写");
        $("#controller").focus();
        return;
    }
    if (op.length < 1) {
        alert("管理员必须填写");
        $("#op").focus();
        return;
    }
    if (oppwd.length < 1) {
        alert("密码必须填写");
        $("#oppwd").focus();
        return;
    }
    if (ou1.length < 1) {
        alert("OU1必须填写");
        $("#ou1").focus();
        return;
    }
    if (ou2.length < 1 && ou3.length > 0) {
        alert("OU3不为空时，OU2不能为空");
        $("#ou2").focus();
        return;
    }
    //拼接
    var dc = domain.toLowerCase().split(".");
    var oupath = "LDAP://" + controller + "/";
    if (ou3.length > 0) {
        oupath += "OU=" + ou3 + ",";
    }
    if (ou2.length > 0) {
        oupath += "OU=" + ou2 + ",";
    }
    oupath += "OU=" + ou1 + ",";
    for (var i = 0; i < dc.length; i++) {
        oupath += "DC=" + dc[i] + ",";
    }
    oupath = oupath.substring(0, oupath.length - 1);

    return oupath;
}

function iniTable(jsonstr) {
    $.each(jsonstr, function (idx, v) {
        var $tr = $("<tr></tr>");
        $tr.append("<td>" + v.account + "</td>");
        $tr.append("<td>" + v.name + "</td>");
        
        $("#tb_result").append($tr);

    });
}